trigger UpdateProspectsPreferredEmailtest on Opportunity (before insert, before update) {
/*  Purpose: Trigger to push the 'Email' field from the Account to the Prospect's preferred email' field on 
             to the Opportunity
    Created by: Sushmita Dey
*/
    
    
    // Initialize below variables in consideration of bulk triggering or mass records update
    map<Id,Id> Oppty2Account = new map<Id,Id>();    
    map<Id,String> Account2Email = new map<Id,String>();    
    
    // Map all opportunities to it's respective Accounts
    for(Opportunity O: Trigger.New){
        Oppty2Account.put(O.Id,O.AccountId);
    }
    // Map all Personal Accounts to it's Email Id
    for(Account A: [Select Id,PersonEmail from Account where Id In:Oppty2Account.values()]){
         Account2Email.put(A.Id, A.PersonEmail);
    }  
    // Update Opportunties' Prospect's Preferred Email with personal email Id from respective Account
    for(Opportunity O: Trigger.New){
         if(Account2Email.get(Oppty2Account.get(O.Id)) != Null){
             if (Account2Email.get(Oppty2Account.get(O.Id)) != O.Prospect_s_Preferred_Email__c){
                 O.Prospect_s_Preferred_Email__c = Account2Email.get(Oppty2Account.get(O.Id));
             }
         } else {
             O.Prospect_s_Preferred_Email__c = '';
         }        
    }
    

}